    /*  
    championBuilder - Champions Online character planner.
    Copyright (C) 2009 Moritz Hartmeier
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You can find the GNU General Public License in the data folder.
    */


package ch.zarzu.champions.builder.gui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.util.LinkedList;

import javax.swing.JLayeredPane;
import javax.swing.SpringLayout;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class BuildSheet extends JLayeredPane {

    private LinkedList<BuildPower> slots;
    private int advantage_points;

    public BuildSheet(Node xml_build, String type) {
        slots = new LinkedList<BuildPower>();
        setName(type + "s");
        
        advantage_points = 0;
        
        NodeList xml_slots = xml_build.getChildNodes();
        for(int i = 0; i < xml_slots.getLength(); i++) {
            if(xml_slots.item(i).getNodeName().equals("slot") &&
               xml_slots.item(i).getAttributes().getNamedItem("type").getNodeValue().contains(type)){
                slots.add(new BuildPower(xml_slots.item(i), type));
            }
            if(xml_slots.item(i).getNodeName().equals("slot") &&
                       xml_slots.item(i).getAttributes().getNamedItem("type").getNodeValue().contains("advantage")){
                advantage_points++;
            }
        }
        SpringLayout layout = new SpringLayout();
        setLayout(layout);
        setBackground(new Color(12, 128, 215));
        
        for(int i = 0; i < slots.size(); i++) {
            add(slots.get(i));
            if(i == 0) {
                layout.putConstraint(SpringLayout.WEST, slots.get(i), 0, SpringLayout.WEST, this);
                layout.putConstraint(SpringLayout.NORTH, slots.get(i), 0, SpringLayout.NORTH, this);
            } else if(i == 1) {
                layout.putConstraint(SpringLayout.WEST, slots.get(i), 0, SpringLayout.EAST, slots.get(i - 1));
                layout.putConstraint(SpringLayout.NORTH, slots.get(i), 0, SpringLayout.NORTH, this);
            } else if(i % 2 == 0) {
                layout.putConstraint(SpringLayout.WEST, slots.get(i), 0, SpringLayout.WEST, this);
                layout.putConstraint(SpringLayout.NORTH, slots.get(i), 0, SpringLayout.SOUTH, slots.get(i - 2));
            } else {
                layout.putConstraint(SpringLayout.WEST, slots.get(i), 0, SpringLayout.EAST, slots.get(i - 1));
                layout.putConstraint(SpringLayout.NORTH, slots.get(i), 0, SpringLayout.SOUTH, slots.get(i - 2));
            }
        }
        
        //set the first slot to active
        slots.get(0).setActive(true);
    }
    
    public LinkedList<BuildPower> getSlots() {
        return slots;
    }
    
    public int getAdvantagePoints() {
        return advantage_points;
    }
}
